#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# 安装 yq 工具
# 需要使用rq工具配置yaml配置文件
function install_yq() {
    local yq_tarball="$PLAY_HOME/frames/lib/yq_linux_amd64.tar.gz"

    # 检查 yq tar.gz 文件是否存在
    if [ ! -f "$yq_tarball" ]; then
        echo "yq tarball 不存在: $yq_tarball"
        echo "请手动下载"
        echo "wget -P $PLAY_HOME/frames/lib/ https://github.com/mikefarah/yq/releases/download/v4.33.2/yq_linux_amd64.tar.gz"
        return 1
    fi

    # 解压 yq
    tar xvf "$yq_tarball" -C "$PLAY_HOME/frames/lib"

    # 检查解压后的 yq 是否存在
    if [ ! -f "$PLAY_HOME/frames/lib/yq_linux_amd64" ]; then
        echo "yq 二进制文件不存在"
        return 1
    fi

    # 改变 yq 二进制文件的权限
    chmod 744 "$PLAY_HOME/frames/lib/yq_linux_amd64"

    # 移动 yq 到 /usr/bin 目录
    mv "$PLAY_HOME/frames/lib/yq_linux_amd64" /usr/bin/

    # 创建软链接
    ln -s /usr/bin/yq_linux_amd64 /usr/bin/yq

    echo "yq 安装成功"
}

function configureFlinkConf()
{
 flinkConfUrl=$1
 masterNode=$2
 localnode=$3
 
 # 配置主节点地址 
 jobmanager_node="jobmanager.rpc.address: $masterNode"
 jobmanager_node_bind="jobmanager.bind-host: 0.0.0.0"
 # 配置web界面绑定地址
 rest_address="rest.address: $masterNode"
 rest_bind_address="rest.bind-address: 0.0.0.0"
 jobmanager_node_bind="taskmanager.bind-host: 0.0.0.0"
 taskmanager_host="taskmanager.host: $localnode"
 
 
 sed -i "s/^jobmanager.rpc.address: localhost/$jobmanager_node/g" $flinkConfUrl
 sed -i "s/^jobmanager.bind-host: localhost/$jobmanager_node_bind/g" $flinkConfUrl
 sed -i "s/^rest.address: localhost/$rest_address/g" $flinkConfUrl
 sed -i "s/^rest.bind-address: localhost/$rest_bind_address/g" $flinkConfUrl
 sed -i "s/^taskmanager.bind-host: localhost/$jobmanager_node_bind/g" $flinkConfUrl
 sed -i "s/^taskmanager.host: localhost/$taskmanager_host/g" $flinkConfUrl

}

# 配置flink，v1.20.0
function configureFlinkConf_4_new() {
    flinkConfFile=$1
    masterNode=$2
    localnode=$3

    # 检查yq是否安装
    if ! command -v yq &> /dev/null; then
        # 安装yq工具
        install_yq
    fi

    # 配置主节点地址
    yq e ".jobmanager.rpc.address = \"$masterNode\"" -i "$flinkConfFile"
    yq e ".jobmanager.bind-host = \"0.0.0.0\"" -i "$flinkConfFile"
    # 配置web界面绑定地址
    yq e ".rest.address = \"$masterNode\"" -i "$flinkConfFile"
    # 配置web界面绑定地址
    yq e ".rest.bind-address = \"0.0.0.0\"" -i "$flinkConfFile"
    # 配置taskmanager
    yq e ".taskmanager.bind-host = \"0.0.0.0\"" -i "$flinkConfFile"
    yq e ".taskmanager.host = \"$localnode\"" -i "$flinkConfFile"

    if [ $? -eq 0 ]; then
        echo "Flink configuration updated successfully."
    else
        echo "Failed to update Flink configuration."
        return 1
    fi
}


function configureMaster()
{
 flinkMasterConf=$1
 masterNode=$2

 sed -i "s/^localhost/$masterNode/g" $flinkMasterConf
}

function configureWorks()
{
 flinkWorkerConf=$1
 installNodes=$2
 
 sed -i '/^localhost/d' $flinkWorkerConf

 #遍历配置Flink Work节点
 OLD_IFS="$IFS" #保存旧的分隔符
 IFS=","
 nodes=($installNodes)
 IFS="$OLD_IFS" # 将IFS恢复成原来的
 for i in "${!nodes[@]}"; do
    echo "${nodes[i]}" >> $flinkWorkerConf
 done
}

function installFlink()
{
 #1.在frames.txt中查看是否需要安装flink
 flinkInfo=`egrep "^flink" $PLAY_HOME/frames.txt`

 flink=`echo $flinkInfo | cut -d " " -f1`
 isInstall=`echo $flinkInfo | cut -d " " -f2`
 flinkNodes=`echo $flinkInfo | cut -d " " -f3` 
 masterNode=`echo $flinkInfo | cut -d " " -f4` 
 node=`hostname`

 #是否安装
 if [[ $isInstall = "true" && $flinkNodes =~ $node ]];then
    
     #2.查看$PLAY_HOME/frames目录下是否有flink安装包
     flinkIsExists=`find $PLAY_HOME/frames -name $flink`

     if [[ ${#flinkIsExists} -ne 0 ]];then

          if [[ ! -d /opt/app ]];then
              sudo mkdir /opt/app && sudo chmod -R 775 /opt/app
          fi

          #删除旧的
          flink_home_old=`find /opt/app -maxdepth 1 -name "flink*"`
          for i in $flink_home_old;do
                sudo rm -rf $i
          done

          #3.解压到指定文件夹/opt/app中
          echo "开始解压flink安装包"
          tar -zxvf $flinkIsExists -C /opt/app >& /dev/null
          echo "flink安装包解压完毕"

          flink_home=`find /opt/app -maxdepth 1 -name "flink*"`

          # 检查Flink版本并配置相应的配置文件
          flink_version=$(echo $flink | grep -oP '(?<=flink-)[^-bin-scala_]+')
          if [[ "$flink_version" =~ ^1\.20\.0$|^[2-9]\.[0-9]+\.[0-9]+$ ]]; then
              # Flink 1.20.0 及之后的版本
              #4.配置flink.yaml文件
              config_file="config.yaml"
              configureFlinkConf_4_new $flink_home/conf/$config_file $masterNode $node
          else
          # Flink 1.20.0 之前的版本
          #4.配置flink-conf.yaml文件
            config_file="flink-conf.yaml"
            configureFlinkConf $flink_home/conf/$config_file $masterNode $node
          fi

          #5.配置masters文件
          configureMaster $flink_home/conf/masters $masterNode

          #6.配置workers文件
          configureWorks $flink_home/conf/workers $flinkNodes

          #7.配置FLINK_HOME
          profile=/etc/profile.d/my_env.sh
          sudo sed -i "/^export FLINK_HOME/d" $profile
          echo "export FLINK_HOME=$flink_home" | sudo tee -a $profile

          #8.配置PATH
          sudo sed -i "/^export PATH=\$PATH:\$FLINK_HOME\/bin/d" $profile
          echo "export PATH=\$PATH:\$FLINK_HOME/bin" | sudo tee -a $profile

          #9.更新/etc/profile文件
          source /etc/profile && echo "更新/etc/profile环境变量完成"

          # 输出提示信息
          echo "----------------------------"
          echo "|   Flink在$node安装成功！   |"
          echo "----------------------------"
     else
         echo "$PLAY_HOME/frames目录下没有flink安装包"
     fi
 else
     echo "Flink不允许安装在当前节点，请检查配置文件！"
 fi

}

installFlink
